package com.dcbeida.activity.applyreturn;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dcbeida.MyApplication;
import com.dcbeida.R;
import com.dcbeida.activity.BaseActivity;
import com.dcbeida.adapter.ApplyReturnAdapter;
import com.dcbeida.bean.ApplyReturn;
import com.dcbeida.bean.Drug;
import com.dcbeida.bean.DrugListBean;
import com.dcbeida.bean.Sales;
import com.dcbeida.bean.SalsesDetail;
import com.dcbeida.listern.DialogListener;
import com.dcbeida.utils.DialogUtils;
import com.dcbeida.widget.ListViewForScrollView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by 150410 on 2017/10/20.
 */

public class ApplyReturnActivity extends BaseActivity implements ApplyReturnContract.View, AdapterView.OnItemClickListener {
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rel_title1)
    RelativeLayout relTitle1;
    @Bind(R.id.et_tkje)
    EditText etTkje;
    @Bind(R.id.tv_num)
    TextView tvNum;
    @Bind(R.id.myList)
    ListViewForScrollView myList;
    @Bind(R.id.tv_tjsq)
    TextView tvTjsq;
    private ApplyReturnPresenter mPresent;
    private Sales.ListBean salesBean;
    private ApplyReturnAdapter adapter;
    ArrayList<String> list;
    private DialogListener listener = new DialogListener() {
        @Override
        public void onCancl(Dialog dialog) {
            dialog.dismiss();
        }

        @Override
        public void onConfirm(Dialog dialog) {
            dialog.dismiss();
            SalsesDetail.ListBean listBean = (SalsesDetail.ListBean) adapter.getmArrayList().get(selecteds.get(0).intValue());
            mPresent.submit(salesBean.getId(), salesBean.getRefundid(), selecteds.size() + "", etTkje.getText().toString().trim(), drugs);
        }
    };
    private String drugs;
    private List<Integer> selecteds;

    @Override
    protected int getLayoutResId() {
        return R.layout.apply_return;
    }

    @Override
    public void setData() {
        super.setData();
        tvTitle.setText(getString(R.string.sqth));
        list = new ArrayList<>();
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            salesBean = (Sales.ListBean) bundle.getSerializable("Sales.ListBean");
        }

        myList.setVerticalScrollBarEnabled(false);// 去掉竖向滚动条
        if(salesBean.getType() !=1) {
            myList.setOnItemClickListener(this);
        }
        adapter = new ApplyReturnAdapter(this);
        myList.setAdapter(adapter);

        mPresent = new ApplyReturnPresenter(this, this);

        mPresent.getSalsesDetailInfo(MyApplication.user.getUid(), salesBean.getId(), "0");

    }


    @OnClick({R.id.img_back, R.id.tv_tjsq})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_tjsq:
                if (TextUtils.isEmpty(etTkje.getText().toString().trim())) {
                    showTip(getString(R.string.qsrtkje));
                    return;
                }

                if (tvNum.getText().toString().trim().equals("0")) {
                    showTip(getString(R.string.qxzypjgm));
                    return;
                }

                list.clear();
                selecteds = adapter.getSelecteds();
                for (int i = 0; i < selecteds.size(); i++) {
                    SalsesDetail.ListBean listBean = (SalsesDetail.ListBean) adapter.getmArrayList().get(selecteds.get(i).intValue());
                    list.add(listBean.getDrugcode());
                }
                drugs = "";
                for (int i = 0; i < list.size(); i++) {
                    if (list.size() == i || i == 0) {
                        drugs += list.get(i);
                    } else {
                        drugs += "," + list.get(i);
                    }
                }

                DialogUtils.showDialogApplyReturn(mContext, selecteds.size() + "", etTkje.getText().toString().trim(), listener, R.string.cancel, R.string.qrtj);


                break;
        }
    }

    @Override
    public void setPresenter(ApplyReturnContract.Presenter presenter) {

    }

    @Override
    public void showTip(String msg) {
        super.showTip(msg);
    }

    @Override
    public void showSalsesDetailList(ArrayList<SalsesDetail.ListBean> list) {
//        ArrayList<SalsesDetail.ListBean> tempList = new ArrayList<>();
//        for (SalsesDetail.ListBean bean : list) {
//            if (bean.getRefundid().equals("0")) {
//                tempList.add(bean);
//            }
//        }
//        adapter.setmArrayList(tempList);


        ArrayList<SalsesDetail.ListBean> tempList = new ArrayList<>();
        DrugListBean listBean = MyApplication.getInstance().getmDrugBean();
        if (listBean == null || listBean.getList() == null)
            return;
        for (Drug drug : listBean.getList()) {
            List<SalsesDetail.ListBean> drugList = new ArrayList<>();
            for (SalsesDetail.ListBean bean : list) {
                if (bean.getRefundid().equals("0")) {
                    if (drug.getDrugid() == bean.getDrugid()) {
                        bean.setDrugName(drug.getDrugname());
                        drugList.add(bean);
                    }
                }
            }
            if (drugList.size() > 0) {
                SalsesDetail.ListBean data = drugList.get(0);
                data.setFirst(true);
                if(salesBean.getType() == 1) {
                    data.setCount(drugList.size());
                }
            }
            tempList.addAll(drugList);
        }
        if(salesBean.getType() == 1) {
            for (int i = 0; i < tempList.size(); i++) {
                adapter.addSelected(i);
            }
            tvNum.setText(adapter.getSelecteds().size() + "");
        }
        adapter.setmArrayList(tempList);

    }

    @Override
    public void showSuccess(ApplyReturn data) {
        showTip(getString(R.string.tjsqcg));
        Intent intent = new Intent();
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (adapter.isItemSelected(position)) {
            adapter.removeSelected(position);
            //count--;

            for (int i = position; i >= 0; i--) {
                SalsesDetail.ListBean bean = adapter.getmArrayList().get(i);
                if (bean.isFirst()) {
                    bean.setCount(bean.getCount() - 1);
                }
            }

        } else {
            adapter.addSelected(position);
            //count++;
            for (int i = position; i >= 0; i--) {
                SalsesDetail.ListBean bean = adapter.getmArrayList().get(i);
                if (bean.isFirst()) {
                    bean.setCount(bean.getCount() + 1);
                    break;
                }
            }

        }

//        ArrayList<SalsesDetail.ListBean> listBeans = adapter.getmArrayList();
//        for (int i = 0; i < listBeans.size(); i++) {
//
//            for (int pos : adapter.getSelecteds()) {
//                if (i == pos) {
//
//                }
//            }
//        }


        adapter.notifyDataSetChanged();

        tvNum.setText(adapter.getSelecteds().size() + "");
    }
}
